package com.whilelife.Wachovia.account;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * Base class for all transaction data
 * Date: 11/3/11
 * Time: 9:52 PM
 * @author Brent Merle
 */
public abstract class Transaction {
  protected static final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
  protected static final String TRANSACTION_REGEX = "^\\d{1,2}\\/\\d{1,2}\\s.*";

  /**
   * Should match any comma-delimited amount
   */
  protected static final Pattern REGEX_AMOUNT = Pattern.compile("\\s+(\\d{1,3}(?:[,]\\d{3})*\\.\\d{2})\\s");

  /**
   * Dates are: MM/DD
   */
  protected static final Pattern REGEX_DATE = Pattern.compile("(\\d{1,2}\\/\\d{1,2})$");

  protected double value;
  protected Date date;
  protected String description;
  
  public Transaction(double val, String date, String descr) {
    this.value = val;
    try {
      this.date = df.parse(date);
    } catch (ParseException e) {
      System.err.println("Couldn't parse date: " + date);
    }
    this.description = descr;
  }

  public Date getDate() {
    return date;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getDescription() {
    return description;
  }

  public double getValue() {
    return value;
  }

  public static class TransactionComparator implements Comparator<Transaction> {
    public int compare(Transaction o1, Transaction o2) {
      return o1.date.compareTo(o2.date);
    }
  }
}
